﻿using System;
using iSun.Web.UI.Controls;
using iSun.Model;
using iSun.BLL.Base;
using iSun.Entity;
using System.Collections.Generic;
using System.Collections;

namespace iSun.WebApp.DesignForm
{
    public partial class FormTableManager : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //setSolution();
        }

        public string FieldInfo
        {
            get
            {
                return setFieldInfo();
            }
        }

        public string Table
        {
            get
            {
                return GetStringFromQueryString("table", "");
            }
        }
        public string TableName
        {
            get
            {
                return GetStringFromQueryString("tablename", "");
            }
        }
        public Guid TableID
        {
            get
            {
                return GetGuidFromQueryString("tableid");
            }
        }
        public string isAddDefault
        {
            get
            {
                return GetStringFromQueryString("AddDefault","0");
            }
        }
        public string SysTable
        {
            get
            {
                SysTable tb = new SysTable();
                tb.iSun_IsNew = true;
                tb.Notes = "";
                tb.TableName = "";
                if (this.TableID != Guid.Empty)
                {
                    tb = new DBTable().GetTableByTableName(Table); 
                    tb.iSun_IsNew = false;
                }
           
                return iSun.Common.Globals.ObjectToJSON(tb);
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string setFieldInfo()
        {
            //string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            BaseEntity instance = iSun.Common.Globals.getEntity("SysFormTableField") as BaseEntity;
            BaseBussnies bInstance = new iSun.BLL.Base.BaseBussnies(instance);
            //bInstance.Order = "iOrder";
            IList<DataListTemplate> list = bInstance.GetEntitys<DataListTemplate>("TableName='" + Table + "'");
            if (list == null)
            {
                return "[]";
            }
            return iSun.Common.Globals.ObjectToJSON(list);
        }


        /// <summary>
        /// 
        /// </summary>
        public string Instance
        {
            get
            {
                SysFormTableField tf = new SysFormTableField(this.Table, this.TableID);
                tf.Notes = "";
                tf.Length = "50";
                tf.Is_PrimaryKey ="N" ;
                tf.Is_Null = "Y";
                tf.iSun_IsNew = true;
                return iSun.Common.Globals.ObjectToJSON(tf);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string strJson
        {
            get
            {
                //if (IndexOf("QueryForm") > -1)
                {
                    iSun.Model.QuerySolution entity = new QuerySolution();
                    return iSun.Common.Globals.ObjectToJSON(entity);
                }
                ////else
                //{
                //    return "";
                //}
            }
        }

        public string FieldList
        {
            get
            {
                string strJson = "";
                BaseBussnies bll = new BaseBussnies();
                IList<TableInfoStruc> list = bll.GetTableInfoStruc(this.Table);
                List<iSun.Entity.SysFormTableField> TFlist = new List<iSun.Entity.SysFormTableField>();
                if (list != null)
                {
                    foreach (var entity in list)
                    {
                        SysFormTableField stf = new SysFormTableField();
                        stf.TableFieldID = Guid.NewGuid();
                        stf.TableID = Guid.NewGuid();
                        stf.TableName = entity.TableName;
                        stf.Is_ForeignKey = entity.PKey == null ? "N" : entity.PKey.Trim();
                        stf.Is_Null = entity.CanNull == null ? "Y" : entity.CanNull.Trim();
                        stf.FieldName = entity.ColName;
                        stf.FiledType = entity.CType;
                        stf.Notes = entity.ColDesc;
                        stf.Length = entity.Length.ToString();
                        stf.ControlsType = "textfield";
                        stf.ColSeq = "0";
                        stf.iSun_IsNew = false;
                        TFlist.Add(stf);
                    }
                    strJson=iSun.Common.Globals.ObjectToJSON(TFlist);
                }
                else 
                {
                    strJson=setFieldInfo();
                }
                return strJson;
            }
        }

        /// <summary>
        /// 设置默认查询
        /// </summary>
        public string DefineCondition
        {
            get;
            set;
        }

        /// <summary>
        /// 解决方案
        /// </summary>
        public string Solution
        {
            get;
            set;
        }
    }
}